//
// Created on 2024-9-17.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include <fcntl.h>
#include <malloc.h>
#include <modbus/modbus.h>
#include "modbus/test_main.h"
#include <string.h>
#include "hilog/log.h"
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include <napi/native_api.h>
// modbus_t *mb;
modbus_t *ctx;
int rc;
int s = -1;
uint16_t *tab_rp_registers;





int modbusInitConnect(const char *ip, int port) {
    // 创建一个modbus-rtu对象，指定串口号，波特率，校验位，数据位，停止位
    // 成功返回指针，否则返回NULL, 会调用malloc申请内存
    //    mb = modbus_new_rtu("/dev/ttySP1", 115200, 'N', 8, 1);  //linux
    //    mb = modbus_new_rtu("COM1", 115200, 'N', 8, 1);   //windows
    // 创建Modbus上下文  modbus-tcp对象，指定IP地址和端口号
    // todo app需要开启访问网络权限
    ctx = modbus_new_tcp(ip, port);
    if (ctx == NULL) {
        OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Modbus", "Unable to create the libmodbus context");
        return -1;
    }

    modbus_set_debug(ctx, TRUE);
    modbus_set_error_recovery(
        ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL);
    modbus_set_response_timeout(ctx, 3000, 0);
    // 连接到服务器(slave设备)
    if (modbus_connect(ctx) == -1) {
        OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Modbus", "{Modbus CONNECT} Connected failed %s", modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    // 连接Modbus主机，成功返回0, 否则返回-1

    // 设置从机地址，成功返回0, 否则返回-1
        int rc = modbus_set_slave(ctx, 0x01);
        if (rc == -1) {
            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Modbus", "{Modbus CONNECT} Invalid slave ID");
            modbus_free(ctx);
            return -1;
        }
     modbus_set_response_timeout(ctx, 666, 0);
    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "mODBUS", "{Modbus CONNECT} Connected OK");
    return 1;
}

int closeConnect() {
    if (ctx == NULL) {
        return 1;
    }
    modbus_close(ctx);
    modbus_free(ctx);
    return 1;
}
uint16_t *receiveData() {
        if(ctx==NULL){
            return 0;
        }

//    modbus_t *ctx;

//    ctx = modbus_new_tcp("192.168.2.27", 502);
//    modbus_set_debug(ctx, TRUE);
//    modbus_set_error_recovery(
//        ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL);
//    modbus_connect(ctx);
//
//    int rc = modbus_set_slave(ctx, 0x01);
//  modbus_set_response_timeout(ctx, 666, 0);
    tab_rp_registers = (uint16_t *)malloc(10 * sizeof(uint16_t));
    memset(tab_rp_registers, 0, 10 * sizeof(uint16_t));

    int regs = modbus_read_registers(ctx, 0, 10, tab_rp_registers);
        if (regs == -1) {
            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Modbus", "Read registers failed:");
        return 0;
        } else {
            float aa = modbus_get_float_abcd(tab_rp_registers);
//            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Modbus", "{Modbus}%{public}d", regs);
//            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Modbus", "{Modbus}%{public}f", aa);
        
//            for (int i = 0; i < regs; i++) {
////                OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00,"Register  %s", tab_rp_registers[i]);
//            
//            }
//        napi_value result;
//        napi_create_uint32(napi_env env, tab_rp_registers, &result)

            return tab_rp_registers;
        }
}

int sendData(int reg_addr, int value){
            if(ctx==NULL){
                return 0;
            }
//    int regs = modbus_write_registers(ctx, 0, 10, tab_rp_registers)
    int regs = modbus_write_register(ctx, reg_addr,value);
        if (regs == -1) {
                OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Modbus", "Read registers failed:");
            return 0;
        } else   {

        return 1;
        }

}